<template>
    <div id="tmpl">
        <!--评论组件-->
        <!--1.0 实现提交评论数据到服务器的静态结构-->
        <div id="postcomment">
            <h3>提交评论</h3>
            <p class="p"></p>
            <textarea placeholder="请输入您要评论的内容..." v-model="usercomment"></textarea>
            <mt-button type="primary" size="large" @click="postComment">发表</mt-button>
        </div>
    
        <!--2.0 实现获取评论数据列表-->
        <div id="list">
            <h3>评论列表</h3>
            <p class="p"></p>
            <div v-for="(item,index) in list" :key="index">
                <div class="title">
                    <span>第{{index + 1}}楼:</span>
                    <span>用户：{{item.user_name}}</span>
                    <span>发表时间：{{item.add_time | dateformat('YYYY-MM-DD HH:mm:ss')}}</span>
                </div>
                <ul class="mui-table-view">
                    <li class="mui-table-view-cell" v-text="item.content"></li>
                </ul>
            </div>
        </div>
    
        <!--3.0 实现获取更多按钮-->
        <mt-button type="danger" size="large" plain @click="getMore" :disabled='disabled'>加载更多</mt-button>
    </div>
</template>
<script>
import common from '../../kits/common.js';
import { Toast } from 'mint-ui';
export default {
    props: ['id'],
    data() {
        return {
            pageindex: 1,
            list: [],
            usercomment: '',
            disabled: false
        };
    },
    created() {
        this.getComment(this.pageindex);
    },
    methods: {
        postComment() {
            if (this.usercomment.trim().length === 0) {
                Toast('评论不能为空');
                return;
            }
            var url = common.apidomain + '/api/postcomment/' + this.id;
            this.$http.post(url, { content: this.usercomment }, { emulateJSON: true }).then(function (res) {
                let data = res.body;
                Toast(data.message);
                this.list = [{
                    "user_name": "匿名用户",
                    "add_time": new Date(),
                    "content": this.usercomment
                }].concat(this.list)
                this.usercomment = '';
            })
        },
        getComment(pageindex) {
            pageindex = pageindex | 1;
            let url = common.apidomain + '/api/getcomments/' + this.id + '?pageindex=' + pageindex;
            this.$http.get(url).then(function (res) {
                let data = res.body;
                if (data.status != 0) {
                    Toast(res.body.message);
                    return;
                }
                this.list = this.list.concat(data.message);
                if (data.message.length < 10) {
                    this.disabled = true;
                }
            })
        },
        getMore() {
            this.pageindex++;
            this.getComment(this.pageindex);
        }
    }
}
</script>
<style lang="css" scoped>
#postcomment {
    padding: 5px;
}

.p {
    height: 1px;
    width: 100%;
    border-bottom: 1px solid rgba(0, 0, 0, 0.3);
}

#list {
    padding: 5px;
}

.title {
    padding: 5px;
    color: #6d6d72;
    font-size: 15px;
    background-color: rgba(0, 0, 0, 0.1);
}
</style>